<template>
  <div>
    <!-- 头部导航 -->
    <van-nav-bar fixed placeholder>
      <template #left>
        <img src="../../assets/toutiao_logo.png" alt="" />
      </template>
      <template #right> <van-icon name="search" size="0.48rem" @click="$router.push('/search')"/> </template
    ></van-nav-bar>
    <!-- 标签栏 -->
    <van-tabs sticky v-model="active" animated offset-top="1.2266667rem">
      <van-tab
        v-for="obj in userChannelList"
        :key="obj.id"
        :title="obj.name"
        :name="obj.id"
        ><ArticeList :channelId="active"></ArticeList
      ></van-tab>
    </van-tabs>
    <!-- 编辑频道+ -->
    <van-icon
      @click="plusClickFn"
      name="plus"
      size="0.37333334rem"
      class="moreChannels"
    />
    <!-- 底部反馈面板 -->
    <van-action-sheet
      v-model="$store.state.homeArticleActionShow"
      :actions="actionArr"
      @select="onSelect"
      :cancel-text="cancelText"
      @cancel="cancelFn"
      @close="actionCloseFn"
      get-container="body"
    />
    <!-- 弹出层组件 -->
    <van-popup v-model="show" class="edit_wrap">
      <!-- 弹出层的主体区域 -->
      <channel-edit
        @closePE="closeFn"
        :userChannelList="userChannelList"
        @addchannelEvent="addChannelFn"
        @removeChannelEvent = "removeChannelFn"
        v-model="active"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannelListAPI, removeChannelAPI, setUserChannelAPI, dislikeArticleAPI, articleReportsAPI } from '@/api'
import ArticeList from './ArticleList.vue'
import { mapState, mapMutations } from 'vuex'
import { Toast } from 'vant'
import channelEdit from './ChannelEdit.vue'
export default {

  data () {
    return {
      active: 0,
      userChannelList: [], // 用户已选频道
      show: false,
      actionArr: [],
      cancelText: '取消'

    }
  },
  computed: {
    ...mapState(['firstActions', 'secondActions', 'actionArtId'])
  },
  methods: {
    ...mapMutations(['changeActionShowMutations']),
    // 点击反馈面板选项
    async onSelect (item) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      if (item.name === '反馈垃圾内容') {
        this.actionArr = this.secondActions
        this.cancelText = '返回'
      } else if (item.name === '不感兴趣') {
        const res = await dislikeArticleAPI(this.actionArtId)
        console.log(res)
        this.changeActionShowMutations(false)
      } else {
        const res = await articleReportsAPI({
          artId: this.actionArtId,
          type: item.value
        })
        console.log(res)
        this.changeActionShowMutations(false)
        setTimeout(() => {
          this.actionArr = this.firstActions
          Toast.success('举报成功')
        }, 200)
      }
    },
    // 反馈面板关闭
    actionCloseFn () {
      this.actionArr = this.firstActions
    },
    cancelFn () {
      if (this.cancelText === '返回') {
        // console.log('111')
        this.changeActionShowMutations(true)
        this.actionArr = this.firstActions
        this.cancelText = '取消'
      }
    },
    // +号频道编辑-点击事件
    plusClickFn () {
      this.show = true
    },
    // 编辑频道页面-关闭弹出层
    closeFn (bool) {
      this.show = bool
    },
    // 添加频道
    async addChannelFn (channelObj) {
      this.userChannelList.push(channelObj)
      const newArr = []
      this.userChannelList.forEach((obj, index) => {
        if (obj.id !== 0) {
          const newObj = { ...obj }
          newObj.seq = index
          delete newObj.name
          newArr.push(newObj)
        }
      })
      const res = await setUserChannelAPI(newArr)
      console.log(res)
    },
    async removeChannelFn (channelId) {
      const res = await removeChannelAPI(channelId)
      console.log(res)

      const index = this.userChannelList.findIndex(obj => obj.id === channelId)
      this.userChannelList.splice(index, 1)
    }
  },
  async created () {
    const res = await getUserChannelListAPI()
    console.log(res)
    this.userChannelList = res.data.data.channels
    this.actionArr = this.firstActions
  },
  components: {
    ArticeList,
    channelEdit
  }
}
</script>

<style scoped lang="less">
/deep/ .van-nav-bar__left img {
  width: 100px;
  height: 30px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
/* 频道编辑弹出层铺满屏幕 */
.edit_wrap {
  width: 100%;
  height: 100%;
}
</style>
